package com.ocm.commerceinteraction.util.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ocm.commerceinteraction.MainActivity;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.model.GroupDetailModel;
import com.ocm.commerceinteraction.ui.ChatActivity;
import com.ocm.commerceinteraction.ui.UserDetailFragment_;
import com.ocm.commerceinteraction.util.MethedUtil;
import com.ocm.commerceinteraction.util.helper.ScreenHelper;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Description:横向动作图片展示
 *
 * @author tl
 * @Date 2015-06-01
 * @Copyright:2015-6-01
 */
public class MyHorizontalActionImgScroll extends LinearLayout
{
	Context context;
	SyncHorizontalScrollView SHScrollView;
	RelativeLayout rl_nav;
	RadioGroup rg_nav_content;
//	public static List<String> imageLists;
	List<GroupDetailModel.DataEntity.UserListEntity> userList;
//	public static List<String> idLists;
	public static String groupUniqIds;
	private int indicatorWidth;
	private ImageView iv_nav_indicator;
	private int tabTitleLenght = 0;// 菜单个数
	private int paddingCount = 50;// 与屏幕左右两边的间离之和(默认40)
	View v;

	int flag;//入口：1课程详情 2，动作计划
	// 检测布局是否加载完成mHandler
	private Handler mHandler = new Handler()
	{
	};
	// 检测时间间隔
	private int detchTime = 5;

	OnClickItemMenuListener itemMenuClickCallBack;// 用户点击事件回调

	/**
	 * 自定义内部接口，用于用户点击最终节点时的事件回调
	 */
	public interface OnClickItemMenuListener
	{
		public void onClickItemMenuListener(int position);
	}

	public void setItemMenuClickCallBack(OnClickItemMenuListener itemMenuClickCallBack)
	{
		this.itemMenuClickCallBack = itemMenuClickCallBack;
	}

//	public MyHorizontalActionImgScroll(Context _context, List<CourseAction> actionList,int flag)
//	{
//		super(_context);
//		context = _context;
//		this.flag = flag;
//		listAction = actionList;
//		tabTitleLenght = listAction.size();
//		if (tabTitleLenght > 3)
//		{
//			tabTitleLenght = 3;
//		}
//		init_layout(context);
//	}

	public MyHorizontalActionImgScroll(Context _context, List<GroupDetailModel.DataEntity.UserListEntity> userList, String groupUniqId, int _paddingCount, int flag)
	{
		super(_context);
		context = _context;
		this.flag = flag;
		this.userList = userList;
//		idLists = idlist;
		groupUniqIds = groupUniqId;
		paddingCount = (int) ScreenHelper.dp2px(_context, _paddingCount);
		tabTitleLenght = userList.size();

//		if (tabTitleLenght > 5)
//		{
			tabTitleLenght = 5;
//		}
		init_layout(context);
	}

	public MyHorizontalActionImgScroll(Context _context, AttributeSet attrs)
	{
		super(_context, attrs);
		context = _context;
		init_layout(context);
	}

	/**
	 * 初始化控件
	 *
	 * @param context
	 */
	void init_layout(Context context)
	{
		v = LayoutInflater.from(context).inflate(R.layout.view_synchorizontalscroll_layout, null, false);
		SHScrollView = (SyncHorizontalScrollView) v.findViewById(R.id.mHsv);
		rg_nav_content = (RadioGroup) v.findViewById(R.id.rg_nav_content);
		iv_nav_indicator = (ImageView) v.findViewById(R.id.iv_nav_indicator);
		iv_nav_indicator.setVisibility(View.GONE);
		rl_nav = (RelativeLayout) v.findViewById(R.id.rl_nav);
		initHScrollMenu();
		v.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		this.addView(v);
	}

	/**
	 * 初始化菜单
	 */
	void initHScrollMenu()
	{
		// 获取分辨率
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);

		// 设置每一个菜单的长度
		indicatorWidth = (dm.widthPixels - paddingCount) / tabTitleLenght;

		ImageView img1 = new ImageView(context);
		ImageView img2 = new ImageView(context);

		SHScrollView.setSomeParam(rl_nav, img1, img2, (Activity) context);

		// 获取菜单布局填充
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		for (int i = 0; i < userList.size(); i++)
		{
			final View v = (View) mInflater.inflate(R.layout.view_bottom_radiogroup_item, null);
			ImageView imageview = (ImageView) v.findViewById(R.id.item_actionimg);
			ImageView levelIv = (ImageView) v.findViewById(R.id.start);
			TextView gourpLevel = (TextView) v.findViewById(R.id.group_level);
			TextView nickName = (TextView) v.findViewById(R.id.tv_username);
			nickName.setText(userList.get(i).getNick());
			//1:群主 2副群主 3临时管理员 4普通成员
			if (userList.get(i).getLevel().equals("1")) {
				gourpLevel.setText("群主");
				levelIv.setImageDrawable(getResources().getDrawable(R.drawable.ic_start_red));
			} else if (userList.get(i).getLevel().equals("2")) {
				gourpLevel.setText("副群主");
				levelIv.setImageDrawable(getResources().getDrawable(R.drawable.ic_start_yellow));
			} else if (userList.get(i).getLevel().equals("3")) {
				gourpLevel.setText("临时管理员");
				levelIv.setImageDrawable(getResources().getDrawable(R.drawable.ic_start_yellow));
			} else {
				gourpLevel.setText("普通成员");
				levelIv.setVisibility(View.GONE);
			}
			imageview.setId(i);
			imageview.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Fragment userDetailFragment = new UserDetailFragment_();
					//add by Eric
					Bundle bundle = new Bundle();
//					bundle.putString("userUniqId", idLists.get(v.getId()) );
					bundle.putString("userUniqId", userList.get(v.getId()).getUser_uniq_id() );
					bundle.putInt("from", MethedUtil.FROM_GROUP );
					bundle.putString("groupUniqId",groupUniqIds);
					userDetailFragment.setArguments(bundle);

					((ChatActivity) context).getSupportFragmentManager()
							.beginTransaction()
							.replace(R.id.content_none_bottom, userDetailFragment).addToBackStack(null).commit();
				}
			});
			v.setId(i);
			v.setLayoutParams(new LayoutParams(indicatorWidth,LayoutParams.MATCH_PARENT));
//			v.setOnClickListener(new OnClickListener()
//			{
//				@Override
//				public void onClick(View v)
//				{
//					// 回调事件响应
//					itemMenuClickCallBack.onClickItemMenuListener(v.getId());
//				}
//			});

            Picasso.with(context).load(RequestAddr.baseUrl + userList.get(i).getIcon()).into(imageview);
			// 初始化滑动下标
			android.view.ViewGroup.LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
			cursor_Params.width = indicatorWidth;
			iv_nav_indicator.setLayoutParams(cursor_Params);
			rg_nav_content.addView(v);
		}
	}

	/**
	 * 进入后初始化菜单选项
	 */
	public void setInitShowChild(final int index)
	{
		/********* 以下代码用来循环检测布局是否初始化完毕 *******/
		Runnable showLayoutRunnable = new Runnable()
		{
			@Override
			public void run()
			{
				// 得到activity中的根元素
				View view = v.findViewById(R.id.rg_nav_content);
				// 如何根元素的width和height大于0说明activity已经初始化完毕
				if (view != null && view.getWidth() > 0 && view.getHeight() > 0)
				{
					// 执行方法
					 rg_nav_content.getChildAt(index).performClick();
					// 停止检测
					mHandler.removeCallbacks(this);
				}
				else
				{ // 如果没有初始化完毕则等待5毫秒再次检测
					mHandler.postDelayed(this, detchTime);
				}
			}
		};
		// 开始检测
		mHandler.post(showLayoutRunnable);
	}
}
